package com.bjpowernode.controller;

import com.bjpowernode.beans.NewsType;
import com.bjpowernode.dto.ResultDTO;
import com.bjpowernode.exception.MyException;
import com.bjpowernode.services.NewsTypeServices;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;



@RestController
@RequestMapping("/admin/newsType")
public class NewsTypeController {

    @Autowired
    NewsTypeServices newsTypeServices;

    @RequestMapping("getAll.action")
    public ResultDTO getAll() {
        ResultDTO resultDTO = new ResultDTO();
        ArrayList<NewsType> all = newsTypeServices.getAll();
        resultDTO.setMess("新闻类型查询成功");
        resultDTO.setData(all);
        return resultDTO;
    }

    @RequestMapping("add.action")
    public ResultDTO add(@Valid NewsType newsType, BindingResult bindingResult) throws MyException {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            throw new MyException(fieldError.getDefaultMessage());
        }
        ResultDTO resultDTO = new ResultDTO();
        newsTypeServices.add(newsType);
        resultDTO.setMess("新闻类型添加成功");
        return resultDTO;
    }

    @RequestMapping("del.action")
    public ResultDTO del(@RequestParam(required = false) Integer id) throws MyException {
         if(id==null){
              throw new MyException("你必须输入删除的新闻类型ID");
         }
         //转型错误报HTTP400，表示请求失败
         //调用业务
         ResultDTO resultDTO = new ResultDTO();
         newsTypeServices.del(id);
         resultDTO.setMess("新闻类型删除成功");
         return resultDTO;
    }

    @RequestMapping("editBefore.action")
    public ResultDTO editBefore(@RequestParam(required = false) Integer id) throws MyException {
        if(id==null){
            throw new MyException("你必须输入修改的新闻类型ID");
        }
        //转型错误报HTTP400，表示请求失败
        //调用业务
        ResultDTO resultDTO = new ResultDTO();
        NewsType newsType = newsTypeServices.get(id);
        if(newsType==null){
            throw new MyException("没有查到新闻类型ID为【"+id+"】的新闻类型");
        }
        resultDTO.setData(newsType);
        resultDTO.setMess("按照新闻类型ID查询成功");
        return resultDTO;
    }


    @RequestMapping(value = "edit.action")
    public ResultDTO edit(@Valid NewsType newsType,BindingResult bindingResult) throws MyException {

        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            //转型错误报HTTP500，需要匹配typeMismatch错误编码
            if(fieldError.getCode().equals(TypeMismatchException.ERROR_CODE) && fieldError.getField().equals("id")){
                throw new MyException("新闻类型ID必须是数字");
            }
            throw new MyException(fieldError.getDefaultMessage());
        }
        //调用业务
        ResultDTO resultDTO = new ResultDTO();
        newsTypeServices.edit(newsType);
        resultDTO.setMess("新闻类型修改成功");
        return resultDTO;
    }



}
